import DataManager from "../manager/DataManager";
import { privacyChannel } from "../Script/privacyChannel";
import BaseLayer from "./Baselayer";


const { ccclass, property } = cc._decorator;

@ccclass
export default class rewardBoxFrame extends BaseLayer {
    @property({ type: [cc.Node], tooltip: '全部宝箱节点' })
    allBoxNode = [];
    @property({ type: cc.Node, tooltip: '底部钥匙节点' })
    keyParentNode = null;
    @property({ type: [cc.Node], tooltip: '单个钥匙节点' })
    keyNodeArr = [];
    @property({ type: cc.Node, tooltip: '底部看广告节点' })
    adContainerNode = null;
    @property({ type: cc.Node, tooltip: '不看视频圆圈' })
    watchAdOff = null;
    @property({ type: cc.Node, tooltip: '看视频圆圈' })
    watchAdOn = null;
    @property({ type: cc.Node, tooltip: '获得钥匙按钮' })
    getKeyBtn = null;
    @property({ type: cc.Node, tooltip: '关闭按钮' })
    closeBtn = null;
    @property({ type: cc.Node, tooltip: '关闭节点' })
    close = null;

    /**拥有的钥匙数量 */
    hasKeyCnt = 3;
    selectBoxId = 0;
    /**全部钥匙数组 */
    allKeyIdx = [0, 1, 2, 3, 4, 5, 6, 7, 8];
    /**拥有钥匙数组 */
    hasKeyArr = [];
    openBoxCnt = 0;
    show(): void {
        this.node.active = true
    }
    onLoad() {

        this.adContainerNode.active = false;
        this.keyParentNode.active = true;
        this.watchAdOn.active = true;
        this.close.active = false;

        switch (privacyChannel.channel) {
            case "oppo":
            case "oppo1":
            case "vivo":
            case "vivo1":
            case "xm":
                if (privacyChannel.getChannel().getCtrl("p1")) {
                    privacyChannel.getChannel().showInter()
                }

                if (privacyChannel.getChannel().getCtrl("p4")) {

                    this.watchAdOn.active = true
                    console.log("控制p4", privacyChannel.getChannel().getCtrl("p4"))
                } else {
                    this.watchAdOn.active = false
                }
                break
        }


        this.allKeyIdx = this.allKeyIdx.sort(() => Math.random() - 0.5);
        for (let i = this.allKeyIdx.length - 1; i >= 0; i--) {
            if (this.hasKeyArr.length < 3) {
                this.hasKeyArr.push(this.allKeyIdx[i]);
                this.allKeyIdx.splice(i, 1);
            } else {
                break;
            }
        }
        for (let i = 0; i < this.allBoxNode.length; i++) {
            if (this.hasKeyArr.indexOf(i) >= 0) {
                this.allBoxNode[i].getChildByName('video').active = false;
            } else {
                this.allBoxNode[i].getChildByName('video').active = true;
            }
        }
        for (let i = 0; i < this.allBoxNode.length; i++) {
            this.allBoxNode[i].on(cc.Node.EventType.TOUCH_END, () => {
                if (this.hasKeyCnt > 0 && this.allBoxNode[i].getChildByName('rewardBox1').active && this.hasKeyArr.indexOf(i) >= 0) {
                    this.selectBoxId = i + 1;
                    this.keyNodeArr[3 - this.hasKeyCnt].getChildByName('yaoshi').active = false;
                    this.hasKeyCnt--;
                    this.updateBoxUi(i);
                    if (this.hasKeyCnt <= 0) {
                        this.showAdContainer();
                    }
                    console.log("钥匙打开宝箱");
                }
                else {
                    if (this.allBoxNode[i].getChildByName('rewardBox1').active) {
                        let me = this;
                        //@ts-ignore
                        window.__prv.showVideo(function () {
                            me.updateBoxUi(i);
                            let idx = me.allKeyIdx.indexOf(i);
                            me.allKeyIdx.splice(idx, 1);
                            console.log("视频打开宝箱: ", this.allKeyIdx, i);
                        })
                    }
                }
            });
        }
        this.watchAdOff.on(cc.Node.EventType.TOUCH_END, () => {
            this.watchAdOn.active = !this.watchAdOn.active;
            if (this.watchAdOn.active) {

                this.closeBtn.getComponent(cc.Label).string = '暂时使用';
            } else {

                this.closeBtn.getComponent(cc.Label).string = '暂不使用';
            }
        });
        this.closeBtn.on(cc.Node.EventType.TOUCH_END, () => {
            if (this.watchAdOn.active) {//看广告获取钥匙
                let me = this;
                //@ts-ignore
                window.__prv.showVideo(function () {
                    me.showGetKeyUI();
                    console.error("点击暂时使用获取钥匙");
                })
            } else {
                this.hide();
            }
        });
        this.getKeyBtn.on(cc.Node.EventType.TOUCH_END, () => {
            let me = this;
            //@ts-ignore
            window.__prv.showVideo(function () {
                me.showGetKeyUI();
                console.error("点击按钮获取钥匙");
            })
        });
        this.close.on(cc.Node.EventType.TOUCH_END, () => {
            this.hide();
        });
        this.closeBtn.active = false;
        if (this.watchAdOn.active) {
            this.closeBtn.getComponent(cc.Label).string = '暂时使用';
        } else {
            this.closeBtn.getComponent(cc.Label).string = '暂不使用';
        }
    }
    /**更新宝箱ui */
    updateBoxUi(idx) {
        this.allBoxNode[idx].getChildByName('rewardBox1').active = false;
        /**给玩家添加金币 */
        let coin = this.getReward();
        this.allBoxNode[idx].getChildByName('rewardNum').getComponent(cc.Label).string = coin;
        this.allBoxNode[idx].getChildByName('video').active = false;
        this.openBoxCnt++;
        if (this.openBoxCnt >= 3) {
            this.close.active = true;
        }
    }
    /**看广告加一把钥匙 */
    showGetKeyUI() {
        if (this.openBoxCnt >= 9) {
            return;
        }
        if (this.hasKeyCnt >= 3) {
            return;
        }
        this.keyParentNode.active = true;
        this.adContainerNode.active = false;
        if (this.hasKeyCnt == 0) {
            this.keyNodeArr[2].getChildByName('yaoshi').active = true;
            this.hasKeyCnt = 1;
        } else if (this.hasKeyCnt == 1) {
            this.keyNodeArr[1].getChildByName('yaoshi').active = true;
            this.hasKeyCnt = 2;
        } else if (this.hasKeyCnt == 2) {
            this.keyNodeArr[0].getChildByName('yaoshi').active = true;
            this.hasKeyCnt = 3;
        }
        for (let i = this.allKeyIdx.length - 1; i >= 0; i--) {
            this.hasKeyArr.push(this.allKeyIdx[i]);
            this.allKeyIdx.splice(i, 1);
            break;
        }
        for (let i = 0; i < this.allBoxNode.length; i++) {
            if (this.hasKeyArr.indexOf(i) >= 0) {
                this.allBoxNode[i].getChildByName('video').active = false;
            } else {
                if (this.allBoxNode[i].getChildByName('rewardBox1').active) {
                    this.allBoxNode[i].getChildByName('video').active = true;
                }
            }
        }
    }
    hide() {
        DataManager.instance.power += this.openBoxCnt
        DataManager.instance.save()

        switch (privacyChannel.channel) {
            case "oppo":
            case "oppo1":
                if (privacyChannel.getChannel().getCtrl("p2")) {
                    privacyChannel.getChannel().showInter(true)
                }

                break
            default:
                privacyChannel.getChannel().showInter(true)
                break
        }

        this.node.destroy();

        // 增加体力值的逻辑
        // let power = User.get(UserData.POWER);
        // let t = 1; // 这里的t值需要根据实际情况调整
        // User.set(UserData.POWER, power + this.openBoxCnt * t);
        // Tip.show("体力+" + this.openBoxCnt * t);
        // Event.emit(GameEvent.POWER_EFFECT, true);

        // //这里的弹窗就是win的初始化的弹窗
        // //@ts-ignore
        // window.__prv.doAction(1, ['vivo']);
        // //@ts-ignore
        // window.__prv.doAction(1, ['oppo']);

    }
    showAdContainer() {
        this.keyParentNode.active = false;
        this.adContainerNode.active = true;
        this.closeBtn.active = true;
    }
    getReward() {
        return 1;
    }
}
